﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Runtime;
using System.Security.Claims;
using System.Text;
using Test.Dto;
using Test.Entity;
using Test.Interface;

namespace Test.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly IJwtHelperAppService _jwtHelperAppService;
        private readonly IUserAppService _userAppService;
        private readonly IOptionsSnapshot<JWTTokenOption> _jwtTokenOption;

        public AccountController(IJwtHelperAppService jwtHelperAppService, IUserAppService userAppService, IOptionsSnapshot<JWTTokenOption> jwtTokenOption)
        {
            _jwtHelperAppService = jwtHelperAppService;
            _userAppService = userAppService;
            _jwtTokenOption = jwtTokenOption;
        }
        [HttpGet]
        public ActionResult<string> GetToken()
        {
            return _jwtHelperAppService.CreateToken();
        }
        //测试没有Token是否能够调用此接口
        [HttpGet]
        public ActionResult<string> GetTest()
        {
            return "Test Authorize";
        }
        [HttpPost]
        public async Task<ActionResult<TokenDto>> Register(string username, string password, string address)
        {
            return await  _userAppService.RegisterAsync(username, password, address);
        }
        [HttpPost]
        public async Task<TokenDto> Login(string username, string password)
        {
            return await _userAppService.LoginAsync(username, password);
        }
    }
}
